package com.lottery.material.controller.portal.transaction;

import com.lottery.material.controller.base.BaseController;
import com.lottery.material.enumeration.ResultEnum;
import com.lottery.material.jedis.JedisClient;
import com.lottery.material.resolver.CustomException;
import com.lottery.material.service.portal.transaction.UserEarnRecordService;
import com.lottery.material.service.portal.user.PortalUserInfoService;
import com.lottery.material.util.ParameterUtil;
import com.lottery.material.util.ResponseResult;
import com.lottery.material.vo.portal.transaction.EarnRecordStatisticsVo;
import com.lottery.material.vo.portal.transaction.EarnRecordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 焦阳
 */
@Api(value = "收益接口", tags = { "收益接口" })
@Controller
@RequestMapping("/portal/userearn")
@Slf4j
public class UserEarnController extends BaseController {
    @Autowired
    UserEarnRecordService userEarnRecordService;

    @Autowired
    PortalUserInfoService portalUserInfoService;

    @Autowired
    @Qualifier("jedisClientCluster")
    private JedisClient jedisClient;

    @ApiOperation(value="收益明细统计接口", notes="收益明细统计接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户唯一标识", required = true, dataType = "String"),
            @ApiImplicitParam(name = "accessToken", value = "访问令牌（登录的用户必传）", required = true, dataType = "String")
    })
    @RequestMapping(value = "/getIncomeStatistics" ,method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult getIncomeStatistics(HttpServletRequest request){
        Map<String, String> reqParamMap = new HashMap<>();
        //解析参数
        if (ParameterUtil.analyticParameter(request, reqParamMap)) {
            throw new CustomException(ResultEnum.PARSE_EXCEPTION);
        }

        //非空判断
        ResponseResult respDate = validateEmpty(reqParamMap);
        if (null != respDate) {
            return respDate;
        }

        ParameterUtil.checkLogin(reqParamMap, jedisClient, portalUserInfoService);

        ResponseResult x = ParameterUtil.checkSign(reqParamMap);
        if (x != null) return x;

        EarnRecordStatisticsVo statistics = portalUserInfoService.findIncomeStatisticsByUserId(reqParamMap.get("userId"));
        return ResponseResult.buildSuccess(statistics);
    }

    @ApiOperation(value="收益明细列表接口", notes="收益明细列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户唯一标识", required = true, dataType = "String"),
            @ApiImplicitParam(name = "accessToken", value = "访问令牌（登录的用户必传）", required = true, dataType = "String"),
            @ApiImplicitParam(name = "queryType", value = "查询类型（1：打赏收益；2：文章收益；3：收益支出）", required = true, dataType = "String"),
            @ApiImplicitParam(name = "page", value = "用于分页（从1开始）", required = true, dataType = "String"),

    })
    @RequestMapping(value = "/getIncomeRecordList" ,method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult getIncomeRecordList(HttpServletRequest request){
        Map<String, String> reqParamMap = new HashMap<>();
        //解析参数
        if (ParameterUtil.analyticParameter(request, reqParamMap)) {
            throw new CustomException(ResultEnum.PARSE_EXCEPTION);
        }

        //非空判断
        ResponseResult respDate = validateEmpty(reqParamMap);
        if (null != respDate) {
            return respDate;
        }

        ParameterUtil.checkLogin(reqParamMap, jedisClient, portalUserInfoService);

        ResponseResult x = ParameterUtil.checkSign(reqParamMap);
        if (x != null) return x;

        String queryType = reqParamMap.get("queryType");
        if (StringUtils.isBlank(queryType)) {
            return ResponseResult.buildResult(ResultEnum.PARAMETER_BLANK_ERROR.getCode(), String.format(ResultEnum.PARAMETER_BLANK_ERROR.getMessage(),"queryType"));
        }
        String page = reqParamMap.get("page");
        if (StringUtils.isBlank(page)) {
            return ResponseResult.buildResult(ResultEnum.PARAMETER_BLANK_ERROR.getCode(), String.format(ResultEnum.PARAMETER_BLANK_ERROR.getMessage(),"page"));
        }
        Map<String,Object> map = new HashMap<>();
        map.putAll(reqParamMap);
        List<EarnRecordVo> earnRecordVos = userEarnRecordService.findIncomeListByUserId(map);
        return ResponseResult.buildSuccess(earnRecordVos);
    }
}
